#pragma once
#include "InetAddr.hpp"
#include "Connection.hpp"
#include "Socket.hpp"
#include "Epoll.hpp"
#include "Channel.hpp"

using namespace kzz;

class Listener : public Connection
{
public:
    Listener(int port = 8080) : _port(port)
    {
        listensocket = std::make_shared<TcpSocket>();
        listensocket->BuildTcpServerSocketMethod(_port);
        _sockfd = listensocket->Fd();
    }
    void Recver() override
    {
        LOG(LogLevel::DBUGER) << "进入Listen模块";
        InetAddr client;
        // std::shared_ptr<Socket> listensocket(std::make_shared<TcpSocket>(_sockfd));

        int client_fd = listensocket->Accept(client)->Fd();
        uint32_t event = (EPOLLIN | EPOLLOUT);
        std::shared_ptr<Connection> chan(std::make_shared<Channel>());
        chan->SetFd(client_fd);
        chan->SetEvent(event);
        // 写入内核
        _woner->AddConnection(chan);
        //
        //_woner->SetConn(chan);
    }

    void Send() override
    {
    }
    public:
    void SetOwner(Reactor *woner)
    {
        _woner = woner;
    }

private:
    std::shared_ptr<Socket> listensocket;
    Reactor *_woner;
    int _port;
};
